@extends('layouts.admin_common') @section('content')

    @if(app('env')=='production')
        <script src="{{ secure_asset('/js/sy_common.js') }}"></script>
    @else
        <script src="{{ asset('/js/sy_common.js') }}"></script>
    @endif

    <div class="breadcrumbs_container">{{
	Breadcrumbs::render('admin.invoice_edit') }}</div>
    <div class="system_tab_line"></div>

    <div class="lyx_search_table">
        <form>

            <div class="row">
                <div class="form-group row col-sm-4">
                    <label for="inputPassword"
                           class="col-sm-4 col-form-label">@lang('symessages.info.clearing_from')</label>
                    <div class="col-sm-8">
                        <select class="form-control" id="lyx_clearing_from">
                            <option value="1" {{$orderInfo['clearing_from'] == 1 ? 'selected':''}}>都度</option>
                            <option value="2" {{$orderInfo['clearing_from'] == 2 ? 'selected':''}}>月まとめ</option>
                        </select>
                    </div>
                </div>

                <div class="form-group row col-sm-4">
                    <label for="inputPassword"
                           class="col-sm-4 col-form-label">@lang('symessages.info.post_address')</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="lyx_p_address" value="{{$orderInfo['add']}}">
                    </div>
                </div>
            </div>
            <div class="row">

                <div class="form-group row col-sm-4">
                    <label for="inputPassword"
                           class="col-sm-4 col-form-label">@lang('symessages.info.company_shop')</label>
                    <div class="col-sm-8" id="lyx_shop_master">
                        {{$orderInfo['name']}}
                    </div>
                </div>

                <div class="form-group row col-sm-4">
                    <label for="inputPassword"
                           class="col-sm-4 col-form-label">@lang('symessages.info.post_address_alias')</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="lyx_p_address_alias"
                               value="{{$orderInfo['add_alias']}}">
                    </div>
                </div>
            </div>

            <div class="row">

                <div class="form-group row col-sm-4">
                    <label for="inputPassword"
                           class="col-sm-4 col-form-label">@lang('symessages.info.postal_code')</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="lyx_p_postal_code"
                               value="{{$orderInfo['postal_code']}}">
                    </div>
                </div>

                <div class="form-group row col-sm-4">
                    <label for="inputPassword"
                           class="col-sm-4 col-form-label">@lang('symessages.info.company_mobile')</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="lyx_p_mobile" value="{{$orderInfo['tel']}}">
                    </div>
                </div>

                <div class="form-check form-check-inline col-sm-4">
                    <label for="inputPassword"
                           class="col-sm-4 col-form-label">@lang('symessages.info.company_fax')</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="lyx_p_fax" value="{{$orderInfo['fax']}}">
                    </div>
                </div>

            </div>


        </form>


        <div class="lyx_table">

            <div class="lyx_alert">

            </div>

            <table class="table table-bordered">
                <thead>
                <tr>
                    <th scope="col">@lang('symessages.info.category')</th>
                    <th scope="col">@lang('symessages.info.product_code')</th>
                    <th scope="col">@lang('symessages.info.product_name')</th>
                    <th scope="col">@lang('symessages.ajax_data.courier_number')</th>
                    <th scope="col">@lang('symessages.ajax_data.deliver_time')</th>
                    <th scope="col">@lang('symessages.info.price')</th>
                    <th scope="col">@lang('symessages.info.out_num')</th>
                    <th scope="col">@lang('symessages.info.total_money')</th>
                </tr>
                </thead>
                <tbody id="lyx_tbody">
                @foreach($orderDetail as $value)
                    <tr>
                        <td>{{$value->category_name}}</td>
                        <td>{{$value->goods_code}}</td>
                        <td>{{$value->goods_name}} </td>
                        <td>{{$value->courier_number}}</td>
                        <td>{{toDate(strtotime($value->deliver_time),'Y-m-d')}}</td>
                        <td width="100px">{{$value->goods_price}}</td>
                        <td>{{$value->num}}</td>
                        <td class="lyx_total_money" data-money="{{$value->goods_price * $value->num}}">¥{{$value->goods_price * $value->num}}</td>
                    </tr>
                @endforeach

                <tr id="lyx_total_tr">
                    <td colspan="6" style="text-align: center">ご請求金額</td>
                    <td colspan="2" id="total_sum_money">¥0</td>
                </tr>

                </tbody>
            </table>
            <div class="row">
                <div class="col-sm-8"></div>
                <div class="col-sm-4">
                    <button class="btn btn-primary" id="lyx_post_submit">提交</button>
                </div>
            </div>
        </div>

    </div>


    <script>

        $(function () {
            var postLineData = [];
            var total_sum_money = 0;//总金额
            var order_id = '{{$order_id}}';
            sumToalMoney();

            $('#lyx_post_submit').on('click', function () {
                postData();
            })


            /**
             * 计算总价，并且把数据放到数据中
             */
            function sumToalMoney() {
                postLineData = [];
                $('.lyx_total_money').each(function () {
                    var j = $(this).data('money');
                    if (j != undefined && j != null && j != '') {
                        total_sum_money = total_sum_money + j;
                    }
                })
                $('#total_sum_money').html('¥' + total_sum_money);
            }


            function postData() {
                $address = $('#lyx_p_address').val();
                $alias = $('#lyx_p_address_alias').val();
                $fax = $('#lyx_p_fax').val();
                $goods_date = $('#lyx_p_goods_date').val();
                $mobile = $('#lyx_p_mobile').val();
                $postal = $('#lyx_p_postal_code').val();
                $weituo_date = $('#lyx_p_weituo_date').val();

                $lyx_clearing_from = $('#lyx_clearing_from').val();
                $company_name = $('#lyx_companys').find("option:selected").text();

                $shop_id = $('.lyx_shop_select').val();
                $shop_name = $('.lyx_shop_select').find("option:selected").text();

                console.log($address, $alias, $fax, $goods_date, $mobile, $postal, $weituo_date, postLineData);


                $.ajax({
                    url: 'save_edit_order',
                    type: 'post',
                    data: {
                        order_id: order_id,
                        address: $address,
                        clearing_from: $lyx_clearing_from,
                        company_name: $company_name,
                        shop_id: $shop_id,
                        shop_name: $shop_name,
                        alias: $alias,
                        fax: $fax,
                        mobile: $mobile,
                        postal: $postal,
                    },
                    success: function (retData) {
                        if (retData.code == 1001) {
                            successAlert('編集に成功する請求処理','invoice_process');
                        } else {
                            lyxAlert('alert-primary', retData.msg, 3000);
                        }
                    },
                    error: function (retData) {
                        var msg = [];
                        $.each(retData.responseJSON.errors, function (index, item) {
                            msg.push(item[0]);
                        })
                        if (!msg[0]) {
                            msg[0] = retData.responseJSON.message;
                        }
                        lyxAlert('alert-primary', msg[0], 3000);
                    }
                })

            }


        })

    </script>
@endsection

